/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.PurchaseBillItemDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class PurchaseBillItemDaoImpl extends BaseDaoImpl
		implements
			PurchaseBillItemDao {
	public List getList(long billId) {
		String sql = "from com.minierp.model.PurchaseBillItem s where s.billId="
				+ billId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.PurchaseBillItem where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	private String getListForSelectSql(long providerId, String billType,
			long tsId, String proName, String proCode, long proTypeId) {
		String str = " purchase_bill_item i left join purchase_bill b on b.id=i.bill_id left join purchase_bill_ext be on b.id=be.id left join product p on i.product_id=p.id left join product_type t on p.product_type_id=t.id where be.enabled=\'Y\' and b.provider_id="
				+ providerId + " and b.tally_set_id=" + tsId;
		if (billType != null && !billType.equals("")) {
			str = str + " and b.bill_type=:billType";
		}

		if (proName != null && !proName.equals("")) {
			str = str + " and (p.product_name like :proName)";
		}

		if (proCode != null && !proCode.equals("")) {
			str = str + " and (p.product_code like :proCode)";
		}

		if (proTypeId != 0L) {
			str = str + " and (t.id=" + proTypeId
					+ " or (t.parent_id_str like \'%;" + proTypeId + ";%\'))";
		}

		return str;
	}

	private void setListForSelectPara(SQLQuery q, String billType,
			String proName, String proCode) {
		if (billType != null && !billType.equals("")) {
			q.setString("billType", billType);
		}

		if (proName != null && !proName.equals("")) {
			q.setString("proName", "%" + proName + "%");
		}

		if (proCode != null && !proCode.equals("")) {
			q.setString("proCode", "%" + proCode + "%");
		}

	}

	public List getListForSelect(long providerId, String billType, long tsId,
			String proName, String proCode, long proTypeId, int startIndex,
			int num) throws Exception {
		String str = this.getListForSelectSql(providerId, billType, tsId,
				proName, proCode, proTypeId);
		String sql = " select i.id as i_id,b.purchase_date,p.product_code,p.product_name,p.unit,i.amount,i.unit_price,i.cost,p.id as p_id from "
				+ str + " order by b.purchase_date desc,b.id desc";
		Session sess = null;

		List arg16;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			this.setListForSelectPara(q, billType, proName, proCode);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg16 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg16;
	}

	public int getListForSelectCount(long providerId, String billType,
			long tsId, String proName, String proCode, long proTypeId)
			throws Exception {
		String str = this.getListForSelectSql(providerId, billType, tsId,
				proName, proCode, proTypeId);
		String sql = " select count(*) from " + str;
		Session sess = null;

		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			this.setListForSelectPara(q, billType, proName, proCode);
			List list = q.list();
			if (!list.isEmpty() && list.get(0) != null) {
				int arg15 = Integer.parseInt(list.get(0).toString());
				return arg15;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}
}